﻿$(function() {
	/**
	 * 设备能耗
	 * */
	var weekpie = echarts.init(document.getElementById('weekpie'));
	optionpie = {
	        tooltip : {
	            trigger: 'item',
	            formatter: function (params, ticket, callback) {
	                if(params.seriesName=="设备状态"){
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"小时 "+"("+params.percent+"%)";
	                }else{
	                	return params.seriesName+" <br/>"+params.name+" : "+params.value+"度 "+"("+params.percent+"%)";
	                }
	            }
	        },
	        legend: {
	        	orient:'vertical',
	        	left:'center',
	        	top:'middle',
	            data: []
	        },
	        backgroundColor:'#e0e0e0',
	        color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
	        series : [{
	        	name:'设备状态',
	            type:'pie',
	            radius:'60%',
	            center:['25%', '45%'],
	            data:[],
	            label:{
	                normal: {
	                	show:false,
		                 formatter: '{b}\n{c}分钟'
		            }
	            }
	        },{
	        	name:'能耗状态',
	            type: 'pie',
	            radius : '60%',
	            center : ['75%', '45%'],
	            data:[],
	            label: {
	                normal: {
	                	show:false,
		                formatter: '{b}\n{c}分钟'
		            }
	            }
	        }]
	    };
	weekpie.setOption(optionpie);
	
	/**
	 * 状态时长
	 * */
    var weekbar1 = echarts.init(document.getElementById('weekbar1'));
    optionbar1 = {
    	    tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        }
    	    },
    	    grid: {
                top: '10%',
                left: '4%',
                right: '4%',
                bottom:'15%',
                containLabel: true
            },
    	    legend: {
    	    	bottom: '30',
    	        data:[]
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#ff6264','#61c0de','#00b19d','#f7aa47','#999933','#0084c5','#ff8463','#9bca62','#fad75f','#003366'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],
		    	splitLine:{
		            	show:false
		        },
		        axisLabel:{
   	    		 formatter: '{value}周'
   	    	 }
    	    },
    	    yAxis: {
    	    	 type: 'value',
    	    	 name:'状态时长(分钟)',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
		    	 splitLine:{
		            	show:false
		            }
    	    },
    	    series: []
    	};
    weekbar1.setOption(optionbar1);
    
    var lineId=0;var equId=0;
    var	startWeek="";
    var	endWeek="";
    var year=moment().year();
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startWeek=value[0];
	       	endWeek=value[1];
	       	getData();
       }
    });
    
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 
    var currentWeek=moment().week();
    var weeks="";
    if(currentWeek<8){
    	weeks=[1,currentWeek];
    }else{
    	weeks=[moment().subtract(7, 'w').week(),currentWeek];
    }
    $('#ss').slider('setValues',weeks);
    var arr=$('#ss').slider('getValues');
    startWeek=arr[0];
    endWeek=arr[1];
    getData();
    function getData(){
    	$.post(basePath+"/single/singlelStartrate/week.json",{"lineId":lineId,"equId":equId,year:year,startTime:startWeek,endTime:endWeek},function(res){
    		vm.$set('item',res);
    		if(res.success){
    			weekpie.setOption({
    				legend:{
    					data:res.pieLegend
    				},
    				series:[{
    					name:'设备状态',
    					data:res.pieSValue
    				},{
    					name:'能耗状态',
    					data:res.pieCValue
    				}]
    			});
    			var series=[];
    		    for(var i = 0;i<res.eachDayValue.length;i++){
    		        series.push({
    		            name: res.eachDayValue[i].name,
    		            stack: '总量',
    		            type: 'bar',
    		            barMaxWidth:40,
    		            data: res.eachDayValue[i].value
    		        });
    		    }
    		    weekbar1.setOption({
    				legend:{
    					data:res.pieLegend
    				},
    				xAxis : [{
        	            data : res.weeks//['10周','11周','12周','13周','14周','15周','16周','17周','18周','19周','20周']
        	        }],
        	        series:series
    			});
    		}else{
    			alert(res.msg);
    		}
    	});
    }
    
  //点击事件
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	//定义时间的表单
	$('#defineTimeWin').dialog({
		title:'自定义时间',
		width:400,
		height:150,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'确定',
			iconCls:'icon-add-new',
			handler:function(){
					var st=$("input[name='startTime']").val();
					var cmax=moment(st, "YYYY").weeksInYear();
					year=st;
					$('#ss').slider({
						max:cmax
					});
					getData();
					$('#defineTimeWin').dialog('close');

			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#defineTimeWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#defineTimeform').form('reset');
		}
	});
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onClick: function(node) {
			if (node.url) {
				lineId=node.id;
				var lineGrid = $('#lines_data');
				lineGrid.datagrid('load',{
					lineId:node.id
				});
				lineGrid.datagrid('getPanel').panel('setTitle',node.text);
			}
		}
	});
	$('#lines_data').datagrid({
		//双击设备时，根据选中设备刷新
		onDblClickRow: function(rowIndex, rowData){
			lineId=rowData.lineId;
			equId=rowData.id;
			var arr=$('#ss').slider('getValues');
		    startWeek=arr[0];
		    endWeek=arr[1];
		    getData();
		    mePanel.dialog('close');
		}
	});
 $("#switchEquipOK").click(function(){
 	var row = $('#lines_data').datagrid('getSelected');
 	if(row){
 		lineId=row.lineId;
 		equId=row.id;
 		var arr=$('#ss').slider('getValues');
 	    startWeek=arr[0];
 	    endWeek=arr[1];
 	    getData();
		$('#switchEquip').dialog('close');
 	}else{
 		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条设备记录!',timeout:3000});
 	}
 });
    
	
});